Control Statements | |
See subtopics: break, continue, do/while, for, for/in, if, if/else, label, return, switch/case, while, and with. |
break | NN 2 IE J1 ECMA 1 |
Stops execution of the current loop and returns control to the next script statement following the end of the current loop. Note that without a label parameter, the scope of the break statement is its own loop. To break out of a nested loop, assign labels to each nested layer, and use the desired label as a parameter with the break statement. See the label statement (available only starting with Navigator 4 and Internet Explorer 4). | |
Syntax break [label] | |
Example See the label statement. |
continue | NN 2 IE J1 ECMA 1 |
Stops execution of the current iteration through the loop and returns to the top of the loop for the next pass (executing the update expression if one is specified in a for loop). If you are using nested loop constructions, assign labels to each nested layer, and use the desired label as a parameter with the continue statement. See the label statement (available only starting with Navigator 4 and Internet Explorer 4). | |
Syntax continue [label] | |
Example outerLoop: for (var i = 0; i <= maxValue1; i++) { for (var j = 0; j <= maxValue2; j++) { if (j*i == magic2) { continue outerLoop } } } |
do/while | NN 4 IE J3 ECMA n/a |
Executes statements in a loop while a condition is true. Because the condition is tested at the end of the loop, the statements inside it are always executed at least one time. It is imperative that the expression that makes up the condition have some aspect of its value potentially altered in the statements. Otherwise, an infinite loop occurs. | |
Syntax do { statements } while (condition) | |
Example var i = 1 do { window.status = "Loop number " + i++ } while (i <= 10) window.status = "" |
for | NN 2 IE J1 ECMA 1 |
A construction that allows repeated execution of statements, usually for a controlled number of times. | |
Syntax for ([initExpression]; [condition]; [updateExpression]) { statements } | |
Example var userEntry = document.forms[0].entry.value var oneChar for (var i = 0; i < userEntry.length; i++) { oneChar = userEntry.charAt(i) if (oneChar < "0" || oneChar > "9") { alert("The entry must be numerals only.") } } |
for/in | NN 2 IE J1 ECMA 1 |
A variation of the regular for loop that can extract the property names and values of an object. | |
Syntax for (varName in objectRef) { statements } | |
Example function showProps() { objName = "image" obj = document.images[0] var msg = "" for (var i in obj) { msg += objName + "." + i + "=" + obj[i] + "\n" } alert(msg) } |
if | NN 2 IE J1 ECMA 1 |
A simple conditional statement that provides one alternate execution path. | |
Syntax if (condition) { statement(s) if true } | |
Example if (myDateObj.getMonth() == 1) { calcMonthLength() } |
if/else | NN 2 IE J1 ECMA 1 |
A conditional statement that provides two execution paths depending on the result of the condition. You can nest another if or if/else statement inside either path of the if/else statement. | |
Syntax if (condition) { statement(s) if true } else { statement(s) if false } | |
Example var theMonth = myDateObj.getMonth() if (theMonth == 1) { monLength = calcLeapMonthLength() } else { monLength = calcMonthLength(theMonth) } |
label | NN 4 IE J3 ECMA n/a |
You can assign a label identifier to any block of executing statements, including control structures. The purpose of the label is to allow break and continue statements within deeply nested control structures to exit to a nested level that may be at levels beyond the scope of the normal break and continue statements. | |
Syntax labelName: | |
Example outerLoop: for (var i = 0; i <= maxValue1; i++) { for (var j = 0; j <= maxValue2; j++) { if (i == magic1 && j == magic2) { break outerLoop } } } |
return | NN 2 IE J1 ECMA 1 |
Stops execution of the current function. A return statement can be located anywhere within the function, including inside control structures. You can optionally specify a value to be returned to the calling statement. This return value can be any JavaScript data type. If a return statement that returns a value is in a loop or other control structure, there must be a return statement for each branch of the execution tree, including a default return statement if execution should reach the main execution scope near or at the end of the function. | |
Syntax return [value] | |
Example function validateNumber(form) { var oneChar for (var i = 0; i < userEntry.length; i++) { oneChar = form.entry.value.charAt(i) if (oneChar < "0" || oneChar > "9") { return false } } return true } |
switch/case | NN 4 IE J3 ECMA n/a |
Provides a shortcut to execution paths for numerous conditions of an expression. | |
Syntax switch (expression) { case label1: statements [break] case label2: statements [break] ... [default: statements] } | |
Example var productList = document.forms[0].prodList var chosenItem = productList.options[productList.selectedIndex].value switch(chosenItem) { case "Small Widget": document.forms[0].price.value = "44.95" break case "Medium Widget": document.forms[0].price.value = "54.95" break case "Large Widget": document.forms[0].price.value = "64.95" break default: document.forms[0].price.value = "Nothing Selected" } |
while | NN 2 IE J1 ECMA 1 |
Executes statements in a loop as long as a condition is true. Because the condition is tested at the beginning of the loop, it is conceivable that under the right conditions, the statements inside the loop do not execute. It is imperative that the expression that makes up the condition have some aspect of its value potentially altered in the statements. Otherwise an infinite loop occurs. | |
Syntax while (condition) { statements } | |
Example var i = 0 while (!document.forms[0].radioGroup[i].checked) { i++ } alert("You selected item number " + (i+1) + ".") |
with | NN 2 IE J1 ECMA 1 |
The with statement adds an object to the scope of every statement nested within. This can shorten the code of some statement groups that rely on a particular object reference. | |
Syntax with (objectRef) { statements } | |
Example with (document.forms[0]) { name1 = firstName.value name2 = lastName.value mail = eMail.value } |